//
//  TVCardInfoView.swift
//  iPhone
//
//  Created by Toj on 11/9/22.
//

import UIKit

class TVCardInfoView: UIView {
    
    @objc private func didTapLink() {
        // print("-->toj: " + #function)
        removeFromSuperview()
    }
    
    @objc private func didTapOtherCard() {
        // print("-->toj: " + #function)
        removeFromSuperview()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupAddSubviews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        setupLayoutSubviews()
    }
    
    private lazy var imageView: UIImageView = {
        let view = UIImageView()
        view.image = "dvb_pre_link_bgimg".to_image
        return view
    }()
    
    private lazy var shadowView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        view.cm_shadow(color: .black, opacity: 0.15, radius: 2, corner: 8)
        return view
    }()
    
    private lazy var titleLabel: UILabel = {
        let view = UILabel()
        view.text = "Congratulations! You can bind a smart card to obtain vip benefits"
        view.numberOfLines = 0
        view.textAlignment = .center
        view.textColor = "#212121".to_color
        view.font = UIFont.boldSystemFont(ofSize: 16.f_scale)
        return view
    }()
    
    private lazy var carInfoView: LinkingCardInforView = {
        let view = LinkingCardInforView()
        view.backgroundColor = .clear
        view.phoneView.lineType = .line
        view.smartcardView.lineType = .line
        return view
    }()
    
    private let linkButtonHeight = 32.ui_scale
    private lazy var linkButton: UIButton = {
        let view = UIButton()
        view.backgroundColor = "#E9CE8D".to_color
        view.setTitle("LINK", for: .normal)
        view.setTitleColor("#775E32".to_color, for: .normal)
        view.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14.f_scale)
        view.addTarget(self, action: #selector(didTapLink), for: .touchUpInside)
        view.cm_cornerRadius = linkButtonHeight * 0.5
        return view
    }()
    
    private lazy var otherButton: UIButton = {
        let view = UIButton()
        let attributedString = "Click here to bind other smart cards".lg_localized.to_attributedString(font: UIFont.systemFont(ofSize: 12.f_scale), color: "#666666".to_color, lineStyle: .underline)
        view.setAttributedTitle(attributedString, for: .normal)
        view.addTarget(self, action: #selector(didTapOtherCard), for: .touchUpInside)
        return view
    }()
    
    
    deinit {
        // print("-->toj: TVCardInfoView " + #function)
    }
}

extension TVCardInfoView {
    
    private func setupAddSubviews() {
        
        addSubview(shadowView)
        addSubview(imageView)
        addSubview(titleLabel)
        addSubview(carInfoView)
        addSubview(otherButton)
        addSubview(linkButton)
    }
    
    private func setupLayoutSubviews() {
        imageView.snp.makeConstraints { make in
            make.centerX.top.equalTo(self)
            make.height.equalTo(294.ui_scale)
            make.width.equalTo(336.ui_scale)
        }
        
        shadowView.snp.makeConstraints { make in
            make.edges.equalTo(imageView)
        }
        
        carInfoView.snp.makeConstraints { make in
            make.center.equalTo(imageView)
            make.left.right.equalTo(imageView).inset(12.ui_scale)
            make.height.equalTo((116).ui_scale)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.centerX.top.equalTo(imageView)
            make.bottom.equalTo(carInfoView.snp.top)
            make.width.equalTo(carInfoView)
        }
        
        linkButton.snp.makeConstraints { make in
            make.left.right.equalTo(imageView).inset(20.ui_scale)
            make.top.equalTo(carInfoView.snp.bottom).offset(25.ui_scale)
            make.height.equalTo(linkButtonHeight)
        }
        
        otherButton.snp.makeConstraints { make in
            make.centerX.width.equalTo(imageView)
            make.top.equalTo(imageView.snp.bottom)
            make.height.equalTo(44.ui_mscale)
        }
    }
}
